package com.easyj.admin.sys.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import com.easyj.base.common.ajax.Result;
import com.easyj.base.controller.BaseController;
import com.easyj.base.exception.EasyjException;
import com.easyj.base.exception.ValidateException;
import com.easyj.base.common.page.Page;
import com.easyj.admin.db.model.Group;
import com.easyj.admin.db.model.GroupUser;
import com.easyj.admin.sys.service.GroupService;
	/**
	* 群组
	*
	 */
	@RestController
	@RequestMapping(value = "/sys/group")
	public class GroupController extends BaseController{

		@Autowired
		private  GroupService groupService;

		/**
		 * get
		 * @param id
		 * @return
		 */
	    @GetMapping("/{id}")
	    public Result get(@PathVariable("id") Long id) {
	        Group entity = groupService.findById(id);
	        return Result.Success(entity);
	    }
		
		/**
		* 新增
		*/
		@PreAuthorize("hasPermission('group','group:all')")
		@PostMapping
		public Result add(@RequestBody @Validated Group entity,BindingResult validResult) throws EasyjException {
			
            if(validResult.hasErrors()) {
				throw new ValidateException("数据不符合要求！");
			} 			
			groupService.insertEntity(entity);
			return Result.Success();
			
		}
		
		/**
		* 修改
		*/
		@PreAuthorize("hasPermission('group','group:all')")
		@PutMapping
		public Result update(@RequestBody @Validated Group entity,BindingResult validResult) throws EasyjException{
			
            if(validResult.hasErrors()) {
				throw new ValidateException("数据不符合要求！");
			}  			
			groupService.updateEntity(entity);
			return Result.Success();
			
		}
		/**
		* 分页查询
		* @param filter
		* @return
		 */
		@PreAuthorize("hasPermission('group','group:all')")
		@PostMapping(value = "findPage")
		public Result findPage(@RequestBody  Group filter) {			
		    if(filter.getPage().getLimit()>MaxLimit) {
				return Result.Error(10009,"超出一次读取最大行数"); 
			}
			Page<Group>  page =groupService.findPage(filter);
			return Result.Success(page);
			
		}
		
        /**
 		 *删除
 		*/
 		@PreAuthorize("hasPermission('group','group:all')")
 		@DeleteMapping("/{id}")
 		public Result delete(@PathVariable("id") Long id) throws EasyjException{ 			
			groupService.deleteById(id);
			return Result.Success();		
 			
 		}
 		
 		@PreAuthorize("hasPermission('group','group:all')")
 		@PostMapping(value="addGroupUsers")
 		public Result addGroupUsers(@RequestBody @Validated List<GroupUser> records ,BindingResult validResult) throws EasyjException {
 			if(validResult.hasErrors()) {
				throw new ValidateException("数据不符合要求！");
			}
 			try {
 			   groupService.addGroupUsers(records);
 			}catch(Exception ex) {
 				throw new EasyjException(EasyjException.UnDefine,"增加群组用户失败！请检查是否已经含有此用户。");
 			}
 			return Result.Success("OK");
 			
 		}
 		
 		@PreAuthorize("hasPermission('group','group:all')")
 		@RequestMapping(value = "deleteGroupUser", method = RequestMethod.POST)
		public Result deleteGroupUser(@RequestBody @Validated GroupUser record ,BindingResult validResult) throws EasyjException{
 			if(validResult.hasErrors()) {
				throw new ValidateException("数据不符合要求！");
			}
 			try {
 				groupService.deleteGroupUser(record);
  			}catch(Exception ex) {
  				throw new EasyjException(EasyjException.UnDefine,"删除群组用户失败！请检查是否含有此用户。");
  			}
 			
			return Result.Success();	
			
		}
 		
 		@RequestMapping(value = "findUsersByGroupId", method = {RequestMethod.POST,RequestMethod.GET})
 		public Result findUsersByGroupId(@RequestParam(value = "id") Long id){					
 	    	return Result.Success(groupService.findUsersByGroupId(id)); 			
 			
 		}
}

